package com.xy.biz.base.domain.bo;

import com.xy.biz.base.domain.enums.QualityGuaranteePeriodUnitEnum;
import com.xy.lang.util.CalendarUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.Objects;

/**
 * 
 * 
 * @author zgy
 * @date 2021-9-22
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
public class GoodsBatchBO {
	
	/**
	 * 货品批次Id
	 */
	private Long goodsBatchId;
	/**
	 * 批次代码
	 */
	private String goodsBatchCode;
	/**
	 * 货主Id
	 */
	private Long goodsOwnerId;
	/**
	 * 浓度
	 */
	private String concentration;
	/**
	 * 浓度单位
	 */
	private String concentrationUnit;
	/**
	 * 货品Id
	 */
	private Long goodsId;
	/**
	 * 货号
	 */
	private String goodsCode;
	/**
	 * 货品长代码
	 */
	private String goodsFullCode;
	/**
	 * 货品简称
	 */
	private String goodsAbbr;
	/**
	 * 生产日期
	 */
	private LocalDate productionDate;
	/**
	 * 到期日期
	 */
	private LocalDate expirationDate;
	/**
	 * 风险日期
	 */
	private LocalDate riskDate;
	/**
	 * 冻结日期
	 */
	private LocalDate freezeDate;
	/**
	 * 备注
	 */
	private String memo;
	/**
	 * 状态：1-正常，2-冻结，3-报废
	 */
	private Integer status;
	/**
	 * 审核状态：0-未定义，1-被拒绝，10-待延期，20-待冻结，21-待冻结，30-待废弃，31-待恢复
	 */
	private Integer auditStatus;
	/**
	 * 审核评语
	 */
	private String auditComment;
	/**
	 * 租户Id，0表示不指定
	 */
	private Long tenantId;
	/**
	 * 创建人用户Id
	 */
	private java.lang.Long createUserId;
	/**
	 * 创建人姓名
	 */
	private java.lang.String createUserName;
	/**
	 * 创建时间
	 */
	private java.time.LocalDateTime createTime;
	/**
	 * 更新人用户Id
	 */
	private java.lang.Long updateUserId;
	/**
	 * 更新人姓名
	 */
	private java.lang.String updateUserName;
	/**
	 * 更新时间
	 */
	private java.time.LocalDateTime updateTime;

	/**
	 * 根据批号分析获取日期信息
	 *
	 * @return
	 */
	public LocalDate fromBatchCode() {
		LocalDate localDate;
		try {
			localDate = CalendarUtils.parseDate(this.goodsBatchCode, "yyyyMMdd");
		} catch (DateTimeParseException e) {
			localDate = null;
		}
		return localDate;
	}

	public void computeExpirationDate(int qualityGuaranteePeriod,
									  QualityGuaranteePeriodUnitEnum qualityGuaranteePeriodUnit) {
		if(productionDate == null) {
			throw new IllegalStateException("The productionDate not set yet!");
		}
		if(Objects.equals(QualityGuaranteePeriodUnitEnum.DAY, qualityGuaranteePeriodUnit)) {
			expirationDate = productionDate.plusDays(qualityGuaranteePeriod);
		} else if(Objects.equals(QualityGuaranteePeriodUnitEnum.MONTH, qualityGuaranteePeriodUnit)) {
			expirationDate = productionDate.plusMonths(qualityGuaranteePeriod);
		} else if(Objects.equals(QualityGuaranteePeriodUnitEnum.YEAR, qualityGuaranteePeriodUnit)) {
			expirationDate = productionDate.plusYears(qualityGuaranteePeriod);
		}
	}

	public void computeRiskDate(int factor,
								QualityGuaranteePeriodUnitEnum factorPeriodUnit) {
		if(expirationDate == null) {
			throw new IllegalStateException("The expirationDate not set yet!");
		}
		if(Objects.equals(QualityGuaranteePeriodUnitEnum.DAY, factorPeriodUnit)) {
			riskDate = expirationDate.plusDays(-factor);
		} else if(Objects.equals(QualityGuaranteePeriodUnitEnum.MONTH, factorPeriodUnit)) {
			riskDate = expirationDate.plusMonths(-factor);
		} else if(Objects.equals(QualityGuaranteePeriodUnitEnum.YEAR, factorPeriodUnit)) {
			riskDate = expirationDate.plusYears(-factor);
		}
	}

	public void computeFreezeDate(int factor,
								QualityGuaranteePeriodUnitEnum factorPeriodUnit) {
		if(expirationDate == null) {
			throw new IllegalStateException("The expirationDate not set yet!");
		}
		if(Objects.equals(QualityGuaranteePeriodUnitEnum.DAY, factorPeriodUnit)) {
			freezeDate = expirationDate.plusDays(-factor);
		} else if(Objects.equals(QualityGuaranteePeriodUnitEnum.MONTH, factorPeriodUnit)) {
			freezeDate = expirationDate.plusMonths(-factor);
		} else if(Objects.equals(QualityGuaranteePeriodUnitEnum.YEAR, factorPeriodUnit)) {
			freezeDate = expirationDate.plusYears(-factor);
		}
	}

}
